package com.wg.msshopsp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wg.msshopsp.entity.*;
import com.wg.msshopsp.service.ShopService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Tag(name = "ShopController", description = "店铺管理")
@RestController
public class ShopController {
    @Autowired
    private ShopService shopService;
    @PostMapping("/shop")
    @Operation(summary = "新增店铺")
    public int insert(@RequestBody Shop shop) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        shop.setCreateTime(sdf.format(new Date()));
        shop.setStatus("0");
        return shopService.save(shop)?1:0;
    }

    @DeleteMapping("/shop")
    @Operation(summary = "删除店铺")
    public int delete(@RequestBody Shop shop) {
        return shopService.removeById(shop)?1:0;
    }

    @PutMapping("/shop")
    @Operation(summary = "修改店铺")
    public int update(@RequestBody Shop shop) {
        return shopService.updateById(shop)?1:0;
    }

    @GetMapping("/shop")
    @Operation(summary = "查询店铺")
    public Shop select(@Parameter Integer id) {
        return shopService.getById(id);
    }


    // 查询店铺下的所有商品
    @GetMapping("/shop/products")
    @Operation(summary = "查询店铺下的所有商品")
    public List<ProductVO> selectProductsByShopId(@Parameter Integer shopId) {
        return shopService.selectProductsByShopId(shopId);
    }

    // 查询所有店铺(管理员)
    @GetMapping("/shop/shops")
    @Operation(summary = "查询所有店铺(管理员)")
    public Page<Shop> queryShops(Shop shop,QueryPageDto pager) {
        return shopService.queryShops(shop, pager);
    }

    // 查询所有店铺带用户信息(管理员)
    @GetMapping("/shop/shopsWithUser")
    @Operation(summary = "查询所有店铺带用户信息(管理员)")
    public IPage<ShopUserDTO> getShopWithUser(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        // 直接返回分页结果
        return shopService.getShopWithUserPage(pageNum, pageSize);
    }

    // 查询店铺,带评分
    @GetMapping("/shop/shopWithScore")
    @Operation(summary = "查询店铺,带评分")
    public ShopScoreVO selectShopWithAvgScoreById(@Parameter Integer shopId) {
        return shopService.selectShopWithAvgScoreById(shopId);
    }

    @GetMapping("/shop/shopsByUserId/{userId}")
    @Operation(summary = "查询用户下的所有店铺")
    public Page<Shop> queryShopsByUserId(@PathVariable("userId") String userId, QueryPageDto pager) {
        System.out.println(pager);
        return shopService.queryShopsByUserId(userId, pager);
    }
}
